<?php
namespace app\admin\model;
use system\core\Model;

class Role extends Model {
    protected $table = 'base_role';

    public function getAll() {
        return $this->db->query("SELECT * FROM {$this->table}")->fetchAll(\PDO::FETCH_ASSOC);
    }
    public function get($id) {
        $stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE id=?");
        $stmt->execute([$id]);
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }
    public function insert($data) {
        $fields = implode(',', array_keys($data));
        $placeholders = implode(',', array_fill(0, count($data), '?'));
        $stmt = $this->db->prepare("INSERT INTO {$this->table} ($fields) VALUES ($placeholders)");
        return $stmt->execute(array_values($data));
    }
    public function update($id, $data) {
        $sets = [];
        foreach($data as $k=>$v) $sets[]="$k=?";
        $stmt = $this->db->prepare("UPDATE {$this->table} SET ".implode(',', $sets)." WHERE id=?");
        return $stmt->execute(array_merge(array_values($data), [$id]));
    }
    public function delete($id) {
        $stmt = $this->db->prepare("DELETE FROM {$this->table} WHERE id=?");
        return $stmt->execute([$id]);
    }
}
